package com.tensquare.friend.service;

import com.tensquare.friend.client.UserClient;
import com.tensquare.friend.dao.FriendDao;
import com.tensquare.friend.dao.NoFriendDao;
import com.tensquare.friend.pojo.Friend;
import com.tensquare.friend.pojo.NoFriend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import util.IdWorker;

import javax.transaction.Transactional;

/**
 * @author: WangYaWei
 * @description:
 * @create: 2019-08-26 16:52
 **/
@Service
@Transactional(rollbackOn = Exception.class)
public class FriendService {
    @Autowired
    private FriendDao friendDao;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private NoFriendDao noFriendDao;

    @Autowired
    private UserClient userClient;

    /**
     * 修改喜欢状态
     * @param userid 用户id
     * @param friendid 好友id
     * @param islike 是否互相喜欢
     */
    public void updateLiek(String userid, String friendid, String islike){
        friendDao.updateLike(userid, friendid, islike);
    }

    /**
     * 添加好友
     * @param userid 用户id
     * @param friendid  好友id
     * @return
     */
    public int addFriend(String userid, String friendid){
        final int count = friendDao.findCountByUseridAndFriendid(userid, friendid);
        if(count == 0){
            //如果之前没有添加过
            Friend friend = new Friend();
            friend.setUserid(userid);
            friend.setFriendid(friendid);

            //如果在非好友表,删除非好友
            noFriendDao.deleteByUseridAndFriendid(userid, friendid);

            //判断是否互相喜欢
            int countByUseridAndFriendid = friendDao.findCountByUseridAndFriendid(friendid, userid);
            if(countByUseridAndFriendid == 0){
                friend.setIslike("0");
            }else{
                friend.setIslike("1");
                friendDao.updateLike(friendid, userid, "1");
            }
            friendDao.save(friend);

            userClient.incincFollowCount(userid, 1);
            userClient.incFansCount(friendid, 1);
            return 1;
        }else{
            return 0;
        }
    }

    /**
     * 添加非好友
     * @param userid 用户id
     * @param friendid  好友id
     */
    public void addNoFriend(String userid, String friendid) {
        NoFriend noFriend = new NoFriend();
        noFriend.setUserid(userid);
        noFriend.setFriendid(friendid);
        noFriendDao.save(noFriend);


        int result = friendDao.deleteByUseridAndFriendid(userid, friendid);
        if(result > 0){
            userClient.incincFollowCount(userid, -1);
            userClient.incFansCount(friendid, -1);
        }
        friendDao.updateLike(friendid, userid, "0");
    }

    /**
     * 删除好友
     * @param userid 用户id
     * @param friendid 好友id
     */
    public void deleteByUseridAndFriendid(String userid, String friendid) {
        //删除好友
        friendDao.deleteByUseridAndFriendid(userid, friendid);
        //修改对方的好友喜欢状态
        friendDao.updateLike(friendid, userid, "0");
        //非好友表添加
        NoFriend noFriend = new NoFriend();
        noFriend.setUserid(userid);
        noFriend.setFriendid(friendid);
        noFriendDao.save(noFriend);

        userClient.incincFollowCount(userid, -1);
        userClient.incFansCount(friendid, -1);
    }
}
